/**
 * @(#)com.etc.util.ResultSetMapper.java
 * @date 2014-8-19 
 */
package com.chj.util;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Date;

/**
 * ResultSet工具类，根据字段名取出对应的数据
 * @author <a href="mailto:zrgxy_1102@163.com">张瑞</a>
 * @version 1.0 2014-8-19
 */
public class ResultSetUtil {
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return String
	 * @throws SQLException
	 */
	public static String getString(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getString(name);
			}
		}
		return null;
	}
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return Object
	 * @throws SQLException
	 */
	public static Object getObject(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getObject(name);
			}
		}
		return null;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return int
	 * @throws SQLException
	 */
	public static int getInt(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getInt(name);
			}
		}
		return 0;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return long
	 * @throws SQLException
	 */
	public static long getLong(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getLong(name);
			}
		}
		return 0;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return double
	 * @throws SQLException
	 */
	public static double getDouble(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getDouble(name);
			}
		}
		return 0;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return BigDecimal
	 * @throws SQLException
	 */
	public static BigDecimal getBigDecimal(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getBigDecimal(name);
			}
		}
		return null;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return float
	 * @throws SQLException
	 */
	public static float getFloat(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getFloat(name);
			}
		}
		return 0;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL，只包含日期
	 * @param rs
	 * @param paramName
	 * @return Date
	 * @throws SQLException
	 */
	public static Date getDate(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				java.sql.Date date =  rs.getDate(name);
				if(date == null){
					return null;
				}
				return new Date(date.getTime());
			}
		}
		return null;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL，只包含时间
	 * @param rs
	 * @param paramName
	 * @return Time
	 * @throws SQLException
	 */
	public static Time getTime(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				java.sql.Time time =  rs.getTime(name);
				return time; 
			}
		}
		return null;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL，包含时间和日期
	 * @param rs
	 * @param paramName
	 * @return Date
	 * @throws SQLException
	 */
	public static Date getDateTime(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				java.sql.Timestamp time =  rs.getTimestamp(name);
				if(time == null){
					return null;
				}
				return new Date(time.getTime()); 
			}
		}
		return null;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return byte
	 * @throws SQLException
	 */
	public static byte getByte(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getByte(name);
			}
		}
		return 0;
	}
	
	/**
	 * 获取ResultSet里面paramName对应的值，如果不存在返回NULL
	 * @param rs
	 * @param paramName
	 * @return short
	 * @throws SQLException
	 */
	public static short getShort(ResultSet rs, String paramName) throws SQLException {
		int count = rs.getMetaData().getColumnCount();
		for(int i=1;i<=count;i++){
			String name = rs.getMetaData().getColumnLabel(i);
			if(name.equalsIgnoreCase(paramName)){
				return rs.getShort(name);
			}
		}
		return 0;
	}
	
}
